import { Injectable } from '@angular/core';
import { ServiceBase } from './base';
import { Router } from '@angular/router';

@Injectable()
export class NavigateService extends ServiceBase {
    private history: any[] = [];
    get History(): any[] { return this.history; }

    constructor() {
        super();
    }

    private router: Router;
    navigate(pathObj) {
        // if (isClear) {
        //     localStorage.removeItem("history");
        //     this.history = [];
        // }
        // let navs = JSON.parse(localStorage.getItem("history"));
        // if (navs && navs.length) {
        //     this.history = navs;
        // }
        // if (pathObj && pathObj.length) {
        //     this.history.push(pathObj);
        //     localStorage.setItem("history", JSON.stringify(this.history));
        //     this.router.navigate(pathObj);
        // }

        if (!this.router) throw "router 没有初始化！";
        if (this.AuthCheck(pathObj[0])) {
            this.router.navigate(pathObj);
        }
    }

    goBack() {
        let navs = JSON.parse(localStorage.getItem("history"));
        if (navs && navs.length) {
            this.history = navs;
        }
        let length = this.history.length;
        if (this.history) {
            if (length > 1) {
                this.navigate(this.history[length - 2]);
                this.history.splice(length - 2, 2);
                localStorage.setItem("history", JSON.stringify(this.history));
            } else {
                localStorage.removeItem("history");
                this.router.navigate(["/"]);
            }
        }
    }

    registRouter(router: Router) {
        this.router = router;
    }

    AuthCheck(path: string) {
        //debug
        // if (!(this.CurrentStore || this.StoreUser || this.StoreUser.ld)) {
        //     //没有缓存的店铺和店长信息，重新回到店长登录页面。/initialization/
        //     this.router.navigate(["initialization/welcome"]);
        //     return false;
        // }
        return true;
    }

    route;
    registerRoute(route) {
        this.route = route;
    }
    onViewDetails(id) {
        this.router.navigate(["../" + id], { relativeTo: this.route });
    }
}